/*******************************************************************************
Title: Individualism during Crisis 
Authors: Bo Bian, Jingjing Li, Ting Xu, Natasha Z. Foutz
Purpose: Produce the following tables
         - Table 7, 8 Panel B
		 - Appendix Table A15, A16, A17 Panel B
*******************************************************************************/

clear all
set more off
set matsize 5000
cap log close
set linesize 200

log using "tables\rep_vaccine", replace

******************************************************************************** 
* Table 7 and Table A15 (COVID vaccination attitude using KFF survey) 
********************************************************************************
use "data\rep_vaccine_KFF_dec.dta", clear
* define outcome variables 
tab q16
g definity_get=(q16==1)
replace definity_get=. if q16>=.

tab q17
g asap=(q17==1)
replace asap=. if q17>=.

tab q11
g eveyone_resp=(q11==2)
replace eveyone_resp=. if q11>=.

* define control variables
g age60=(age>=60)
g white=(race==1)
g male=(gendervar==1)

g essen_worker=(essential==1)
g hc_worker=(hcworker2<=3 & hcworker2<.)
g insurance=(coverage==1 & coverage<.)
g regular_flu=(q24==1 & q24<.)
g know_covid=(test1==1 & test1<.)
g die_covid=(test3==1 & test3<.)
g chronic=(chroniccovid<=3 & test3<.)

global ctrl_demographics age60 white male
global ctrl_covid essen_worker hc_worker insurance regular_flu know_covid die_covid chronic
global fe_demographics_rest hhadults marital receduc income /*categorical variables*/

* Table 7 - column (1)
eststo definity_get: quietly reghdfe definity_get tfe_bin $ctrl_demographics $ctrl_covid [pw=weight], ab(state ${fe_demographics_rest}) cl(county)
codebook county if e(sample)

* Table 7 - column (2)
eststo asap: quietly reghdfe asap tfe_bin $ctrl_demographics $ctrl_covid [pw=weight], ab(state ${fe_demographics_rest}) cl(county)
codebook county if e(sample)

* Table 7 - column (3)
eststo eveyone_resp: quietly reghdfe eveyone_resp tfe_bin $ctrl_demographics $ctrl_covid [pw=weight], ab(state ${fe_demographics_rest}) cl(county)
codebook county if e(sample)

* Table 7
esttab definity_get asap eveyone_resp, ar2 star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) label br mtitle title(Table 7) replace

* Table A15 
tabstat definity_get asap eveyone_resp if e(sample), stat(N mean sd min p50 max) columns(s) varwidth(20) format(%9.3gc)


******************************************************************************** 
* Table A16 (kindergarten vaccine - us county level) 
********************************************************************************
use "data\rep_vaccine_kindergarten.dta", clear 

* define control variables 
global ctrl lnpop medianage pct_male pct_white ctr_marriedratio ctr_BAandabove std_median_income 

* Table A16
eststo clear
foreach y of varlist vacc_rate_*_w5 {
	eststo `y': quietly reghdfe `y' tfe_bin $ctrl, ab(state_id#year) cl(fips)
}
esttab vacc*, ar2 star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) br mtitle title(Table A16) replace

codebook fips if e(sample)


******************************************************************************** 
* Table 8 Panel B, Table 17 Panel B (country-level vaccination attitude)
********************************************************************************
use "data\rep_vaccine_country_level.dta", clear 

* define outcome variables 
tab vaccine
g get_if_available=(vaccine>=4)
tab business4
g get_if_rec_by_employer=(business4>=4)

* define control variables
g ln_covidcases=ln(covidcases)
g ln_coviddeaths=ln(coviddeaths)

foreach x in gender age_group universal_edu world_wide sickwithcovid trusthealth  {
	destring `x', replace
}

destring age, replace force
g age60=(age>=60)
tab gender
g male=(gender==1)
g sickcovid=(sickwithcovid==1)

* define fixed effects and control variable 
global fe_demographics gender age_group universal_edu world_wide
global fe_covidrelated sickwithcovid trusthealth 
global ctrl1 lnpop lndensity polity ln_covidcases ln_coviddeaths

* Table 8 Panel B 
global fe_demographics universal_edu world_wide
global ctrl1 sickcovid trusthealth ln_covidcases ln_coviddeaths
global ctrl2 lnpop lndensity polity 

quietly reghdfe idv distM_UK age60 male $ctrl1 $ctrl2, ab(${fe_demographics}) cl(iso2)
quietly sum idv if e(sample)
estadd scalar mean_y = r(mean)
estadd scalar sd_y = r(sd)
est store first
quietly reghdfe get_if_available idv age60 male $ctrl1 $ctrl2, ab(${fe_demographics}) cl(iso2)
est store ols1
quietly ivreghdfe get_if_available age60 male $ctrl1 $ctrl2 (idv=distM_UK), ab(${fe_demographics}) cl(iso2)
quietly sum get_if_available if e(sample)
estadd scalar mean_y = r(mean)
estadd scalar sd_y = r(sd)
estadd scalar widstatkp = cond(e(widstat)<., e(widstat), -100)
est store iv1
quietly reghdfe get_if_rec_by_employer idv age60 male $ctrl1 $ctrl2, ab(${fe_demographics}) cl(iso2)
est store ols2
quietly ivreghdfe get_if_rec_by_employer age60 male $ctrl1 $ctrl2 (idv=distM_UK), ab(${fe_demographics}) cl(iso2)
quietly sum get_if_rec_by_employer if e(sample)
estadd scalar mean_y = r(mean)
estadd scalar sd_y = r(sd)
estadd scalar widstatkp = cond(e(widstat)<., e(widstat), -100)
est store iv2

estout first ols1 iv1 ols2 iv2, /*
			*/ cells(b(fmt(%9.3f) star) se(fmt(%9.3f) par(`"="("' `")""'))) /*
			*/ keep (idv distM_UK age60 male $ctrl1) order (idv distM_UK age60 male $ctrl1) /*
			*/ stats(N r2_a r2_a_within widstatkp N_clust mean_y sd_y depvar absvars, fmt(%9.0fc %9.3f %9.3f %9.3f %9.0fc %9.3f %9.3f) labels ("N" "Adj R2" "Adj R2 Within" "F_stat" "N_clust" "Dep. Mean" "Dep. sd"  "Dep. Var." "FE")) starlevels(* 0.10 ** 0.05 *** 0.01) /*
			*/ delimiter() end() legend collabels(none) varlabels(_cons Constant) title(Table 8 Panel B) 

			
* Table 17 Panel B (summary stat)
tabstat get_if_available get_if_rec_by_employer if e(sample), stat(N mean sd min p50 max) columns(s) varwidth(20) format(%9.3gc)

log close 


